<?php

// Created by Khanh Nam
class JobsNotesController extends Controller{
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions				
             'users'=>array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    public function actionIndex($jobid){
        $job=Jobs::model()->findByPk($jobid);
        $this->render('index',array('job'=>$job));
    }
    public function actionSaveBatch(){
        if(isset($_POST['jobid']) && isset($_POST['data'])){
            $jobid=$_POST['jobid'];
            $data=$_POST['data'];
            foreach($data as $item){
                if(isset($item[0]) && $item[0]=='delete'){
                    Yii::app()->db->createCommand()->delete('tbl_jobs_notes', 'jobid=:jobid AND id=:id', array(
                        'jobid'=>$jobid,
                        'id'=>$item[1],
                    ));
                }else{
                    $model=NULL;
                    if(isset($item['id']) && $item['id']!=0){
                        $model=JobsNotes::model()->findByPk($item['id']);                        
                        if($model!=NULL){
                            $model->dateofnote=$item['dateofnote'];
                            $model->note=$item['note'];                      
                            //$model->jobid=$item['jobid'];
                        }
                    }else{
                        $model=new JobsNotes;
                        $model->dateofnote=$item['dateofnote'];
                        $model->note=$item['note'];   
                        $model->jobid=$item['jobid'];
                    }
                    if($model!=NULL){
                        $model->dateofnote=$item['dateofnote'];
                        $model->note=$item['note'];                            
                        if($model->save()){
                            echo 'Save successful';
                        }else{
                            echo json_encode($model->getErrors());
                        }
                    }
                }
            }
        }
    }
    public function actionUpdate($id){
        $model=JobsNotes::model()->findByPk($id);
        if($model==NULL) return false;
        if(isset($_POST)){
            $model->attributes=$_POST;
            if($model->save()){
                
                echo json_encode(array(
                    'success'=>true,
                    'msg'=>'Save successful'
                ));
            }else{
                echo json_encode(array(
                    'success'=>false,
                    'errors'=>$model->errors
                ));
            }
        }
    }
    public function actionCreate($jobid=NULL){
        $model=new JobsNotes;
        
        
        if(isset($_POST)){
            
            $model->attributes=$_POST;
            $model->status=-1;
            
            $model->jobid=$jobid;
            if($model->save()){
                echo json_encode(array(
                    'success'=>true,
                    'msg'=>'Save successful'
                ));
            }else{
                echo json_encode(array(
                    'success'=>false,
                    'errors'=>$model->errors
                ));
            }
        }
        
    }
    public function actionDelete($id){
        $note=JobsNotes::model()->findByPk($id);
        $note->delete();
        Yii::app()->end();
    }
    public function actionList($jobid,$page=0,$sort=NULL){
        if($page>0) $page--;
            $order=CJSON::decode($sort);
         
            $data=new CActiveDataProvider('JobsNotes',array(
                'criteria'=>array(
                    'condition'=>'jobid=:jobid',
                    'params'=>array(
                        'jobid'=>$jobid,                     
                    ),
                    'order'=>isset($order)?$order[0]['property'].' '.$order[0]['direction']:NULL
                ),
                'pagination'=>array(
                    'pageSize'=>20,
                    'currentPage'=>$page
                )
            ));
            $rs=array();
                $rs['totalCount']=$data->getTotalItemCount();
            if($data!=NULL && count($data->getData())>0){
                
                foreach($data->getData() as $i=>$item){                                       
                    $rs['data'][$i]=$item->getAttributes();                 
                }
                
            }
            echo json_encode($rs);
            Yii::app()->end();
    }
}
?>

